#include "imageItemWidget.h"
#include <QLabel>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPixmap>
#include <QClipboard>
#include <QApplication>
#include <QDebug>
#include <QScreen>
#include <QWindow>
#include <QMouseEvent>

/**
 * 图片展示组件
 * @brief ImageItemWidget::ImageItemWidget
 * @param item
 * @param parent
 */

ImageItemWidget::ImageItemWidget(const ClipboardItem& item, QWidget *parent)
    : QWidget(parent), item(item), imageLabel(new QLabel(this)),
      titleLabel(new QLabel("图片", this)),
      copyButton(new QPushButton("", this)),
      deleteButton(new QPushButton("", this)),
      favoriteButton(new QPushButton("",this)),
      favoriteTextLayout(new QHBoxLayout()),
      mainLayout(new QVBoxLayout(this)),
      buttonLayout(new QHBoxLayout())
{
    copyButton->setStyleSheet(
                "QPushButton { "
                "image: url(:/icon/icon/copy_button.png);"
                "width: 70px;"
                "height: 30px;"
                "min-width: 70px;"
                "min-height: 30px;"
                "max-width: 70px;"
                "max-height: 30px;"
                "background-color: transparent;"
                "}"
                "QPushButton:hover { "
                "image: url(:/icon/icon/copy_button_hover.png);"
                "}"
                "QPushButton:pressed { "
                "image: url(:/icon/icon/copy_button_pressed.png);"
                "}"
                );
    deleteButton->setStyleSheet(
                "QPushButton { "
                "image: url(:/icon/icon/delete_button.png);"
                "width: 70px;"
                "height: 30px;"
                "min-width: 70px;"
                "min-height: 30px;"
                "max-width: 70px;"
                "max-height: 30px;"
                "background-color: transparent;"
                "}"
                "QPushButton:hover { "
                "image: url(:/icon/icon/delete_button_hover.png);"
                "}"
                "QPushButton:pressed { "
                "image: url(:/icon/icon/delete_button_pressed.png);"
                "}"
                );

    // 设置标题标签的字体和大小
    titleLabel->setStyleSheet("QLabel {font-size: 18px; font-family: 'Dengxian'; font-weight: bold;}"); // 设置标题为粗体
    titleLabel->setContentsMargins(5,5,5,0);

    QString imagePath = item.getDisplayText();

    QPixmap pixmap;

    QImage image = item.getContent().value<QImage>(); // 从 QVariant 中提取 QImage
    pixmap = QPixmap::fromImage(image); // 将 QImage 转换为 QPixmap

    if (!pixmap.isNull()) {
        imageLabel->setPixmap(pixmap.scaled(300, 300, Qt::KeepAspectRatio));  // 设置缩略图大小
    } else {
        imageLabel->setText("无法加载图片");
    }

    // 获取主屏幕的分辨率
       QScreen *screen = QGuiApplication::primaryScreen();
       QRect screenGeometry = screen->geometry();
       int screenWidth = screenGeometry.width();
       int screenHeight = screenGeometry.height();
       imageLabel->setFixedWidth(screenWidth*0.13);
       imageLabel->setMaximumWidth(screenWidth*0.13);
       imageLabel->setMaximumHeight(screenHeight*0.10);

    // 设置布局
    buttonLayout->addWidget(copyButton);
    buttonLayout->addWidget(deleteButton);
    copyButton->show();

    //button下外边距
    buttonLayout->setContentsMargins(0,0,0,5);

    imageLabel->setAlignment(Qt::AlignHCenter);

    favoriteTextLayout->addWidget(titleLabel);
    favoriteTextLayout->addStretch();
    favoriteTextLayout->addWidget(favoriteButton);

    mainLayout->addLayout(favoriteTextLayout);
    mainLayout->addWidget(imageLabel);  // 添加图片标签
    mainLayout->addLayout(buttonLayout); // 添加按钮布局

    setLayout(mainLayout);

    this->installEventFilter(this);

    // 连接信号和槽
    connect(copyButton, &QPushButton::clicked, this, &ImageItemWidget::onCopyClicked);
    connect(deleteButton, &QPushButton::clicked, this, &ImageItemWidget::onDeleteClicked);
    connect(favoriteButton, &QPushButton::clicked, this, &ImageItemWidget::onFavoriteClicked);

    // 添加收藏按钮的样式设置
    QString favoriteImageUrl = item.isFavorite() ? 
        ":/icon/icon/favorite_button_pressed.png" : 
        ":/icon/icon/favorite_button.png";
        
    favoriteButton->setStyleSheet(
        "QPushButton { "
        "image: url(" + favoriteImageUrl + ");"
        "width: 15px;"
        "height: 15px;"
        "min-width: 15px;"
        "min-height: 15px;"
        "max-width: 15px;"
        "max-height: 15px;"
        "background-color: transparent;"
        "border: none;"
        "}"
    );
}

// 事件过滤器，用于捕获双击事件
bool ImageItemWidget::eventFilter(QObject* obj, QEvent* event) {
    if (obj == this && event->type() == QEvent::MouseButtonDblClick) {
        QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
        // 获取 mainLayout 的相对位置并转换为全局位置
        QPoint globalPos = this->mapToGlobal(mainLayout->geometry().topLeft());
        QRect mainLayoutRect(globalPos, mainLayout->geometry().size());

        // 检查双击位置是否在 mainLayout 区域内
        if (mainLayoutRect.contains(mouseEvent->globalPos())) {
            onGetDetailDoubleCliked();
            return true;  // 事件已处理
        }
    }
    return QWidget::eventFilter(obj, event);
}

ClipboardItem ImageItemWidget::getItem() const {
    return item;  // 返回存储的 ClipboardItem
}

void ImageItemWidget::onCopyClicked() {
    emit copyClicked();  // 发射信号
}

void ImageItemWidget::onDeleteClicked() {
    emit deleteClicked();  // 发射信号
}

void ImageItemWidget::onGetDetailDoubleCliked() {
    emit getDetailDoubleCliked();
}

void ImageItemWidget::onFavoriteClicked() {
    emit favoriteClicked();
    item.setFavorite(!item.isFavorite());  // 切换收藏状态
    updateFavoriteButtonStyle();  // 立即更新按钮样式
}

void ImageItemWidget::updateFavoriteButtonStyle() {
    QString favoriteImageUrl = item.isFavorite() ? 
        ":/icon/icon/favorite_button_pressed.png" : 
        ":/icon/icon/favorite_button.png";
        
    favoriteButton->setStyleSheet(
        "QPushButton { "
        "image: url(" + favoriteImageUrl + ");"
        "width: 15px;"
        "height: 15px;"
        "min-width: 15px;"
        "min-height: 15px;"
        "max-width: 15px;"
        "max-height: 15px;"
        "background-color: transparent;"
        "border: none;"
        "}"
    );
}
